//
//  TweetView.m
//  Echo
//
//  Created by Cospring on 14/12/16.
//  Copyright (c) 2014年 sina. All rights reserved.
//

#import "TweetView.h"
#import "UIConfig.h"
#import "ApiConfig.h"
#import "Tweet.h"
#import "Pic.h"
#import "User.h"
#import "CurrentUser.h"
#import "Comment.h"
#import "SDWebImageManager.h"
#import "bottomCommentBar.h"

@implementation TweetView {
    UIView *shadowsView;
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame tweet:(Tweet *)tweet
{
    self = [super initWithFrame:frame];
    if (self) {
        self.tweet = tweet;
        
        CGRect tweetFrame = CGRectMake(0, 0, frame.size.width, frame.size.height - COMMENTVIEW_HEIGHT);
        
        self.tweetScrollView = [[UIScrollView alloc] initWithFrame:tweetFrame];
        self.tweetScrollView.bounces = YES;
        self.tweetScrollView.showsHorizontalScrollIndicator = NO;
        self.tweetScrollView.showsVerticalScrollIndicator = NO;
        
        self.contentView = [[UIView alloc] initWithFrame:tweetFrame];
        self.contentView.layer.borderWidth = 0.5;
        UIColor *borderColor = [UIColor colorWithRed:200/255.0 green:200/255.0 blue:200/255.0 alpha:1];
        //        self.layer.borderColor = [UIColor lightGrayColor].CGColor;
        self.contentView.layer.borderColor = borderColor.CGColor;
        [self.tweetScrollView addSubview:self.contentView];
        [self addSubview:self.tweetScrollView];
        
        [self renderContentView];
        
        _buttomWrapView = [[UIView alloc] initWithFrame:CGRectMake(-TWEET_LEFT_MARGIN, self.frame.size.height - COMMENTVIEW_HEIGHT, SCREEN_WIDTH, COMMENTVIEW_HEIGHT)];
        
        NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"buttomCommentBar" owner:nil options:nil];
        bottomCommentBar *commentBarView = [subviewArray objectAtIndex:0];
        commentBarView.layer.borderWidth = 0.5;
        commentBarView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        commentBarView.frame = CGRectMake(0, 0, _buttomWrapView.frame.size.width, _buttomWrapView.frame.size.height);
        
//        [commentBarView setNeedsUpdateConstraints];
        
        [_buttomWrapView addSubview:commentBarView];
        [self addSubview:_buttomWrapView];
        
//        [self.contentView addSubview:_buttomWrapView];
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showCommentVC:)];
        [commentBarView.commentView addGestureRecognizer:tap];
        
//        commentBarView.layer.borderWidth = 0.5;
//        commentBarView.layer.borderColor = [UIColor lightGrayColor].CGColor;
        
        
        //        [self.tweetScrollView setContentOffset:CGPointZero];
    }
    return self;
}


- (void)renderContentView
{
    CGFloat offsetY = 0;
    
    CGRect tweetLabelRect = CGRectMake(TWEET_LEFT_PADDING, TWEET_TOP_PADDING, TWEET_VIEW_WIDTH, 0);
    UILabel *tweetLabel = [[UILabel alloc] initWithFrame:tweetLabelRect];
    tweetLabel.numberOfLines = 0;
    tweetLabel.font = TWEET_TEXT_FONT;
    tweetLabel.userInteractionEnabled = YES;
    //        tweetLabel.lineBreakMode = NSLineBreakByCharWrapping;
    
    NSMutableAttributedString *text = [[NSMutableAttributedString alloc] initWithString:_tweet.text];
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.lineSpacing = 10;
    //        style.lineBreakMode = NSLineBreakByCharWrapping;
    style.lineBreakMode = NSLineBreakByWordWrapping;
    [text addAttribute:NSParagraphStyleAttributeName value:style range:NSMakeRange(0, text.length)];
    
    tweetLabel.attributedText = text;
    //        tweetLabel.text = _tweet.text;
    CGRect frame = tweetLabel.frame;
    frame.size.width = TWEET_VIEW_WIDTH;
    tweetLabel.frame = frame;
    [tweetLabel sizeToFit];
    [self.contentView addSubview:tweetLabel];
    
    offsetY += tweetLabel.frame.origin.y + tweetLabel.frame.size.height + 20;
    CGFloat imageViewY = tweetLabel.frame.origin.y + tweetLabel.frame.size.height + 20;
    
    NSUInteger count = [_tweet.pics count];
    //    NSMutableArray *imageViewArray = [[NSMutableArray alloc] initWithCapacity:count];
    
    for (int i = 0; i < count; i++) {
        Pic *pic = _tweet.pics[i];
        NSURL *url = [[NSURL alloc] initWithString:pic.url];
        
        [[SDWebImageManager sharedManager] downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
            
            NSLog(@"%ld, %ld",(long)receivedSize, (long)expectedSize);
            
        } completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
            
            NSLog(@"YES: %@", imageURL);
            
        }];
        
        NSURL *imageUrl = [[NSURL alloc] initWithString: pic.url];
        NSData *imageData = [NSData dataWithContentsOfURL:imageUrl];
        UIImage *image = [[UIImage alloc] initWithData:imageData];
        
        CGFloat zoom = TWEET_VIEW_WIDTH / image.size.width;
        
        CGRect imageViewRect = CGRectMake(16, imageViewY, TWEET_VIEW_WIDTH, image.size.height * zoom);
        
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:imageViewRect];
        imageView.image = image;
        [self.imageViewList addObject:imageView];
        
        [self.contentView addSubview:imageView];
        
        imageViewY += imageView.frame.size.height + 10;
    }
    
    CGFloat infoViewY = imageViewY + TWEET_BOTTOM_PADDING;
    
    UIImage *authorImage = [UIImage imageNamed:@"author"];
    CGFloat infoViewHeight = authorImage.size.height + 5;
    
    CGRect infoViewRect = CGRectMake(TWEET_LEFT_PADDING, infoViewY, TWEET_VIEW_WIDTH, infoViewHeight);
    _infoView = [[UIView alloc] initWithFrame:infoViewRect];
    UIView *authorView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, TWEET_VIEW_WIDTH*0.55, infoViewHeight)];
    UIView *timeView = [[UIView alloc] initWithFrame:CGRectMake(TWEET_VIEW_WIDTH*0.55, 0, TWEET_VIEW_WIDTH*0.45, infoViewHeight)];
    [_infoView addSubview:authorView];
    [_infoView addSubview:timeView];
    
    
    UIImageView *authorImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, authorImage.size.width, authorImage.size.height)];
    authorImageView.image = authorImage;
    [authorView addSubview:authorImageView];
    
    _authorNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(authorImageView.frame.origin.x + authorImageView.frame.size.width + 5, 0, authorView.frame.size.width - authorImageView.frame.size.width, infoViewHeight)];
    _authorNameLabel.font = [UIFont systemFontOfSize:16];
    _authorNameLabel.textColor = [UIColor lightGrayColor];
    _authorNameLabel.text = _tweet.owner.screen_name;
    [authorView addSubview:_authorNameLabel];
    
    
    _timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(timeView.frame.size.width - 90, 0, 90, infoViewHeight)];
    NSString *timeString = _tweet.created_time;
    NSRange range = [timeString rangeOfString:@" "];
    if (range.location <= timeString.length - 1) {
        timeString = [timeString substringToIndex:NSMaxRange(range)];
    }
    
    NSLog(@"%@",timeString);
    _timeLabel.text = timeString;
    //        _timeLabel.text = _tweet.created_time;
    
    _timeLabel.font = [UIFont systemFontOfSize:16];
    _timeLabel.textColor = [UIColor lightGrayColor];
    _timeLabel.textAlignment = NSTextAlignmentRight;
    
    CGSize labelSize = [_timeLabel sizeThatFits:CGSizeMake(90, CGFLOAT_MAX)];
    //        NSLog(labelSize);
    NSLog(@"width = %f, height = %f", labelSize.width, labelSize.height);
    CGRect rect = _timeLabel.frame;
    rect.origin.x = timeView.frame.size.width - labelSize.width;
    rect.size = labelSize;
    _timeLabel.frame = rect;
    [timeView addSubview:_timeLabel];
    
    UIImage *tagImage = [UIImage imageNamed:@"history"];
    UIImageView *tagImageView = [[UIImageView alloc] initWithFrame:CGRectMake(_timeLabel.frame.origin.x - tagImage.size.width - 5, 3, tagImage.size.width, tagImage.size.height)];
    tagImageView.image = tagImage;
    [timeView addSubview:tagImageView];
    
    [self.contentView addSubview:_infoView];
    
    CGFloat totalHeight = _infoView.frame.origin.y + _infoView.frame.size.height + 10;
    
    CGRect contentViewFrame = self.contentView.frame;
    contentViewFrame.size.height = totalHeight;
    self.contentView.frame = contentViewFrame;
    
    if (totalHeight < self.frame.size.height) {
        //            totalHeight = self.frame.size.height + 1;
        CGFloat height = self.frame.size.height + 1 - totalHeight;
        CGFloat y = totalHeight;
        shadowsView = [[UIView alloc] initWithFrame: CGRectMake(0, y, TWEET_FRAME_WIDTH, height)];
        
        [self.tweetScrollView addSubview:shadowsView];
        totalHeight = self.frame.size.height + 1;
        //            CGRect contentViewFrame = self.contentView.frame;
        //            contentViewFrame.size.height = totalHeight;
        //            self.contentView.frame = contentViewFrame;
    }
    
    self.tweetScrollView.contentSize = CGSizeMake(TWEET_VIEW_WIDTH, totalHeight);
}


//- (void)setTweet:(NSString *)tweet
//{
//
//}

- (TweetView *)changeTweet:(Tweet *)tweet
{
    _tweet = tweet;
    
    for (UIView *view  in self.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    [self renderContentView];
    
    return self;
    
}

- (void)showCommentVC:(UITapGestureRecognizer *)sender
{
    NSLog(@"%@", sender);
    [self.commentDelegate showCommentVC:_tweet];
}



@end










